package com.ui
{
	import com.engine.AwayEngine;
	import com.engine.SurfaceResize;
	import com.greensock.TweenLite;
	import com.manager.Vision;
	import com.model.CarDataRemote;
	import com.model.MenuXMLData;
	import com.vo.CarVo;
	import com.vo.PartVo;
	
	import feathers.controls.Button;
	import feathers.controls.ButtonGroup;
	import feathers.data.ListCollection;
	import feathers.events.ItemEvent;
	
	import flash.net.drm.AddToDeviceGroupSetting;
	
	import starling.display.DisplayObjectContainer;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.ResizeEvent;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;

	/**
	 * 顶部的导航栏菜单
	 */	
	public class MenuFace extends SurfaceResize
	{
		private static var menu:MenuFace;
		public static function getInstance():MenuFace{
			if(menu== null) menu = new MenuFace();
			return menu;
		}
		
		private var menuGroup:ButtonGroup;//一级菜单
		private var minorGroup:ButtonGroup;//二级菜单
		private var itemGroup:ButtonGroup;//三级菜单
		
		private var minorX:Number = 0;//二级菜单对齐坐标
		
		public function show():void{
			initFace();
			addGroupListeners();
		}
		protected override function resize():void
		{
			itemGroup.x = AwayEngine.senceWidth / 2 - itemGroup.width / 1.2;
			minorGroup.x = AwayEngine.senceWidth / 2 - minorGroup.width / 1.2;
			var x:Number = AwayEngine.senceWidth / 2 - menuGroup.width;
			TweenLite.to(menuGroup,.2,{x:x,y:30});
		}
		
		private var contain:Sprite;
		private function initFace():void
		{
			if(contain == null){
				contain = new Sprite();
				initGroup();
			}
			Vision.addView(Vision.UI,contain);
		}		
		
		private function initGroup():void
		{
			itemGroup = new ButtonGroup();
			itemGroup.y = -30;
			itemGroup.gap = 0;
			itemGroup.direction = ButtonGroup.DIRECTION_HORIZONTAL;
			contain.addChild(itemGroup);
			minorGroup = new ButtonGroup();
			minorGroup.y = -30;
			minorGroup.direction = ButtonGroup.DIRECTION_HORIZONTAL;
			minorGroup.gap = 0;
			contain.addChild(minorGroup);
			menuGroup = new ButtonGroup();
			menuGroup.y = -50;
			contain.addChild(menuGroup);
			menuGroup.direction = ButtonGroup.DIRECTION_HORIZONTAL;
			menuGroup.dataProvider = getTitleList();
			menuGroup.addEventListener(ResizeEvent.RESIZE,addToStage);
			menuGroup.width = 500;
			//开始移动
		}
		
		private function addToStage(e:Event):void
		{
			menuGroup.removeEventListener(ResizeEvent.RESIZE,addToStage);
			menuGroup.gap = 0;
			resize();
		}
		
		private function addGroupListeners():void
		{
			menuGroup.addEventListener(ItemEvent.ITEM_TOUCH,onMenuTouch);
			minorGroup.addEventListener(ItemEvent.ITEM_TOUCH,onMinorTouch);
			itemGroup.addEventListener(ItemEvent.ITEM_TOUCH,onItemTouch);
			contain.addEventListener(TouchEvent.TOUCH,onTouch);
		}
		/**
		 * 获取标题
		 * @return 
		 */		
		private function getTitleList():ListCollection{
			var list:Array = [];
			var tList:Vector.<String> = MenuXMLData.getTitleList();
			
			for each (var title:String in tList) 
			{
				list.push({ label: title});
			}
			return new ListCollection(list);
		}
		/**
		 * 获取二级条目数据
		 * @return 
		 */		
		private function getMinorList(menuIndex:int):ListCollection{
			return new ListCollection(MenuXMLData.getMinorData(menuIndex));
		}
		/**
		 * 获取三级条目数据
		 * @return 
		 */		
		private function getItemList(menuIndex:int,minorIndex:int):ListCollection{
			return new ListCollection(MenuXMLData.getItemData(menuIndex,minorIndex));
		}
		
		private function onMenuTouch(e:ItemEvent):void
		{
			if(e.touch.phase == TouchPhase.HOVER){
				var btn:Button = e.item as Button;//目标选中的实例
				minorX = btn.x;
				
				var lc:ListCollection = getMinorList(e.selectedIndex);
				minorGroup.dataProvider = lc;
				minorGroup.gap = 0;
				
				TweenLite.to(minorGroup,.2,{x:menuGroup.x + minorX,y:60});
//				TweenLite.to(_image,.5,{height:400});
				if(itemGroup.y == 90){
					TweenLite.to(itemGroup,.2,{x:menuGroup.x + minorX,y:90});
					itemGroup.dataProvider = getItemList(e.selectedIndex,0);
				}
			}
		}
		
		private function onMinorTouch(e:ItemEvent):void
		{
			if(e.touch.phase == TouchPhase.HOVER){
				var btn:Button = e.item as Button;//目标选中的实例
				var itemX:Number = btn.x + minorX;
				TweenLite.to(itemGroup,.2,{x:menuGroup.x + itemX,y:90});
				var data:Object = e.selectedItem;
				
				itemGroup.dataProvider = getItemList(data.menuIndex,e.selectedIndex);
				itemGroup.gap = 0;
			}
		}
		/**
		 * 最后获取part数据列表
		 * @param e
		 */		
		private function onItemTouch(e:ItemEvent):void
		{
			if(e.touch.phase == TouchPhase.BEGAN){
				var data:Object = e.selectedItem;
				showPart(data.menuIndex,data.id);
			}
		}
		
		public function showPart(menuIndex:int,itemID:String):void{
			var cvo:CarVo = ModelSelect.getInstance().carVo;
			if(cvo != null){
				var partDataList:Vector.<PartVo> = 
					CarDataRemote.getPartList(cvo.carID,menuIndex,itemID);
				//获取部件列表
				_menuIndex = menuIndex;
				_itemID = itemID;//记录起来
			}
			PartFace.getInstance().partRefresh(partDataList);
		}
		private function onTouch(e:TouchEvent):void{
			if(e.interactsWith(menuGroup) == false &&
				e.interactsWith(minorGroup) == false && e.interactsWith(itemGroup) == false){
				TweenLite.to(minorGroup,.2,{y:-40});
				TweenLite.to(itemGroup,.2,{y:-40});
//				TweenLite.to(_image,.5,{height:1});
			}
		}
		private var _menuIndex:int;
		public function get menuIndex():int
		{
			return _menuIndex;
		}
		private var _itemID:String;
		public function get itemID():String
		{
			return _itemID;
		}
		/**
		 * 重新刷新选择的目标
		 * @param _carVo
		 */
		public function refreshPart():void
		{
			showPart(_menuIndex,_itemID);
		}
	}
}